import { lazy } from 'react';
import {
  UserOutlined,
  DashboardOutlined,
  SettingOutlined,
  ShoppingOutlined,
  OrderedListOutlined,
} from '@ant-design/icons';

// 懒加载页面组件
const Dashboard = lazy(() => import('../pages/Dashboard'));
const UserManagement = lazy(() => import('../pages/UserManagement'));
const UserProfile = lazy(() => import('../pages/UserProfile'));
const SystemSettings = lazy(() => import('../pages/SystemSettings'));
const RoleManagement = lazy(() => import('../pages/RoleManagement'));
const MenuManagement = lazy(() => import('../pages/MenuManagement'));
const ProductList = lazy(() => import('../pages/ProductList'));
const ProductCategories = lazy(() => import('../pages/ProductCategories'));
const OrderList = lazy(() => import('../pages/OrderList'));
const OrderDetail = lazy(() => import('../pages/OrderDetail'));

export interface RouteItem {
  key: string;
  path: string;
  label: string;
  icon?: React.ComponentType;
  component?: React.ComponentType;
  children?: RouteItem[];
  hideInMenu?: boolean;
  permission?: string[];
}

export const routes: RouteItem[] = [
  {
    key: 'dashboard',
    path: '/dashboard',
    label: '仪表板',
    icon: DashboardOutlined,
    component: Dashboard
  },
  {
    key: 'user',
    path: '/user',
    label: '用户管理',
    icon: UserOutlined,
    children: [
      {
        key: 'user-list',
        path: '/user/list',
        label: '用户列表',
        component: UserManagement
      },
      {
        key: 'user-profile',
        path: '/user/profile/:id',
        label: '用户详情',
        component: UserProfile,
        hideInMenu: true
      }
    ]
  },
  {
    key: 'products',
    path: '/products',
    label: '商品管理',
    icon: ShoppingOutlined,
    permission: ['product_manager'],
    children: [
      {
        key: 'product-list',
        path: '/products/list',
        label: '商品列表',
        component: ProductList
      },
      {
        key: 'product-categories',
        path: '/products/categories',
        label: '商品分类',
        component: ProductCategories
      }
    ]
  },
  {
    key: 'orders',
    path: '/orders',
    label: '订单管理',
    icon: OrderedListOutlined,
    permission: ['order_manager'],
    children: [
      {
        key: 'order-list',
        path: '/orders/list',
        label: '订单列表',
        component: OrderList
      },
      {
        key: 'order-detail',
        path: '/orders/detail/:id',
        label: '订单详情',
        component: OrderDetail,
        hideInMenu: true
      }
    ]
  },
  {
    key: 'system',
    path: '/system',
    label: '系统管理',
    icon: SettingOutlined,
    permission: ['admin'],
    children: [
      {
        key: 'role',
        path: '/system/role',
        label: '角色管理',
        component: RoleManagement
      },
      {
        key: 'menu',
        path: '/system/menu',
        label: '菜单管理',
        component: MenuManagement
      },
      {
        key: 'settings',
        path: '/system/settings',
        label: '系统设置',
        component: SystemSettings
      }
    ]
  }
];

// 扁平化路由，用于路由配置
export const flattenRoutes = (routes: RouteItem[]): RouteItem[] => {
  const result: RouteItem[] = [];
  
  const flatten = (items: RouteItem[]) => {
    items.forEach(item => {
      if (item.component) {
        result.push(item);
      }
      if (item.children) {
        flatten(item.children);
      }
    });
  };
  
  flatten(routes);
  return result;
};

// 根据权限过滤路由
export const filterRoutesByPermission = (routes: RouteItem[], permissions: string[]): RouteItem[] => {
  return routes.filter(route => {
    if (route.permission && !route.permission.some(p => permissions.includes(p))) {
      return false;
    }
    
    if (route.children) {
      route.children = filterRoutesByPermission(route.children, permissions);
      // 如果过滤后子路由为空，且当前路由没有组件，则过滤掉该路由
      if (route.children.length === 0 && !route.component) {
        return false;
      }
    }
    
    return true;
  });
};